package gui.controller;

import gui.view.SizeChoiceBox;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * 
 * @author Felix Schliephacke
 * 
 */
public class SizeChangeListener implements ItemListener {

	private Object oldValue;
	private final SizeChoiceBox sizeChoiceBox;
	private final PropertyChangeSupport support;

	public SizeChangeListener(final SizeChoiceBox box) {
		this.sizeChoiceBox = box;
		this.support = new PropertyChangeSupport(this.sizeChoiceBox);
	}

	public void addPropertyChangeListener(final PropertyChangeListener listener) {
		this.support.addPropertyChangeListener(listener);
	}

	@Override
	public void itemStateChanged(final ItemEvent arg0) {
		if (arg0.getStateChange() == ItemEvent.DESELECTED) {
			this.oldValue = arg0.getItem();
		} else if (arg0.getStateChange() == ItemEvent.SELECTED) {
			this.support.firePropertyChange(PropertyChangeConstants.CHANGESIZE
					.toString(), this.oldValue, arg0.getItem());
			this.oldValue = arg0.getItem();
		}
	}

	public void removePropertyChangeListener(
			final PropertyChangeListener listener) {
		this.support.removePropertyChangeListener(listener);
	}

}
